package com.kingtsoft.study.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.model.StudyHomeworkDetail;
import com.kingtsoft.study.persist.StudyHomeworkDetailMapper;
import com.kingtsoft.study.service.StudyHomeworkDetailService;
import com.kingtsoft.study.util.FileUtil;

/**
 * Title:
 * Description:
 * Copyright: Copyright (c) 2017-03-30 下午3:01
 * Company: wondersgroup.com
 *
 * @author jerry.pan(潘健雷)
 * @version 1.0
 */
@Service
public class StudyHomeworkDetailServiceImpl implements StudyHomeworkDetailService {

    @Autowired
    StudyHomeworkDetailMapper homeworkDetailMapper;

    /**
     * 获取作业
     *
     * @param id 作业主键
     * @return 作业
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public StudyHomeworkDetail load(String id) {
        return homeworkDetailMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过老师作业表主键和学生主键获取作业
     *
     * @param homeworkId 老师作业表主键
     * @return 作业
     * @author jerry.pan
     * @date 2017-04-01 12:39:56
     */
    @Override
    public StudyHomeworkDetail loadByHomeworkIdAndStudentId(String homeworkId, String studentId) {
        return homeworkDetailMapper.selectByHomeworkIdAndStudentId(homeworkId, studentId);
    }

    /**
     * 分页获取作业的列表
     *
     * @return pageInfo
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public PageInfo<StudyHomeworkDetail> listPage(PageInfo page, StudyHomeworkDetail studyHomeworkDetail) {
        PageHelper.startPage(page);
        List<StudyHomeworkDetail> list = homeworkDetailMapper.listAll(studyHomeworkDetail);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 获取作业的列表
     *
     * @return list
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public List<StudyHomeworkDetail> listAll(StudyHomeworkDetail studyHomework) {
        return homeworkDetailMapper.listAll(studyHomework);
    }


    /**
     * 增加作业
     *
     * @param studyHomeworkDetail 作业
     * @author jerry.pan
     * @date 2017-03-31 16:11:29
     */
    @Override
    public StudyHomeworkDetail add(StudyHomeworkDetail studyHomeworkDetail) {
        homeworkDetailMapper.insert(studyHomeworkDetail);
        return studyHomeworkDetail;
    }

    /**
     * 修改作业
     *
     * @param studyHomeworkDetail 作业
     * @author jerry.pan
     * @date 2017-03-31 16:11:29
     */
    @Override
    public StudyHomeworkDetail edit(StudyHomeworkDetail studyHomeworkDetail) {
        homeworkDetailMapper.updateByPrimaryKeySelective(studyHomeworkDetail);
        return studyHomeworkDetail;
    }

    /**
     * 保存上传文件
     *
     * @author jerry.pan
     * @date 2017-04-01 15:38:22
     */
    public StudyHomeworkDetail saveUpload(StudyHomeworkDetail studyHomeworkDetail){
        //保存到作业表
        homeworkDetailMapper.updateByPrimaryKeySelective(studyHomeworkDetail);
        //返回
        return studyHomeworkDetail;
    }

    /**
     * 删除上传文件
     *
     * @param
     * @author jerry.pan
     * @date 2017-04-01 15:38:22
     */
    public Map deleteFile(String id) {
        Map resultMap = Maps.newHashMap();
        StudyHomeworkDetail homeworkDetail = this.load(id);
        if (homeworkDetail == null) {
            resultMap.put("describable", DescribableEnum.FILE_NOT_EXIST);
            return resultMap;
        }
        //1.删除硬盘中的文件
        String relativePath = homeworkDetail.getPath();
        try {
            String fullPath = FileUtil.getPath(relativePath);
            FileUtil.deleteFile(fullPath);
        } catch (IOException e) {
            resultMap.put("describable", DescribableEnum.FILE_DEL_ERROR);
            return resultMap;
        }
        //2.删除表中的数据
        homeworkDetail.setFilename(null);
        homeworkDetail.setFileSize(null);
        homeworkDetail.setSuffix(null);
        homeworkDetail.setPath(null);
        homeworkDetailMapper.updateByPrimaryKey(homeworkDetail);
        resultMap.put("describable", DescribableEnum.SUCCESS);
        return resultMap;
    }

    /**
     * 查询已经提交的作业
     *
     * @param page
     * @param studyHomeworkDetail
     * @return page info
     * @author jerry.pan
     * @date 2017-04-05 08:40:11
     */
    public PageInfo<StudyHomeworkDetail> listCommittedPage(PageInfo page, StudyHomeworkDetail studyHomeworkDetail) {
        PageHelper.startPage(page);
        List<StudyHomeworkDetail> list = homeworkDetailMapper.listCommitted(studyHomeworkDetail);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
